package leetcode.editor.cn.q1_300.q;

/**
 * @author qidi
 * @date 2021-11-22 12:12:31
 */
class Q0043_MultiplyStrings {

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public String multiply(String num1, String num2) {
            if (num1.equals("0") || num2.equals("0")) return "0";


            int[] arr = new int[num1.length() + num2.length()];

            int[] arr1 = new int[num1.length()];
            int[] arr2 = new int[num2.length()];

            for (int i = 0; i < arr1.length; i++) {
                arr1[i] = num1.charAt(i) - 48;
            }

            for (int i = 0; i < arr2.length; i++) {
                arr2[i] = num2.charAt(i) - 48;
            }

            for (int i = arr1.length - 1; i >= 0; i--) {
                int tmp = 0;
                for (int j = arr2.length - 1; j >= 0; j--) {
                    int cur = tmp + arr2[j] * arr1[i] + arr[i + j + 1];
                    tmp = cur / 10;
                    arr[j + i + 1] = cur % 10;
                }
                arr[i] = tmp;
            }

            StringBuilder s = new StringBuilder();
            for (int i : arr) {
                if (s.length() == 0 && i == 0) continue;
                s.append(i);
            }
            return s.toString();
        }
    }
    //leetcode submit region end(Prohibit modification and deletion)


    public static void main(String[] args) {
        Solution solution = new Q0043_MultiplyStrings().new Solution();
        System.out.println(solution.multiply("123", "456"));
        System.out.println(solution.multiply("0", "0"));
    }
}